<?php
/*
 * message控制器  
 * Tobey
 * */
require_once "baseController.class.php";
class messageController extends baseController{
	public function __construct(){
		parent::__construct();
		session_start();
		$state = $_SESSION["isTeacher"];
		session_write_close();
		if($state == "true"){
			$data = $this->message(0,"非法操作!!","");
			$this->ajaxReturn($data);
			unset($data);
			exit;
		}
	}
	/*
	获取登录者(学生)的所有通知信息
	ajax  本地
	 */
	public function messages(){
		if(IS_AJAX && IS_LOCAL){
			session_start();
			$stu_id = $_SESSION["id"];
			session_write_close();
			$sql = D();
			$d["message"] = $sql->table(array("message"=>""))->where(array("stu_id"=>$stu_id))->field(array("m_id","m_status","m_content","m_date","cla_id"))->order("m_date desc")->select();
			if($d["message"]){
				$message_len = count($d["message"]);
				$table = array("class"=>"");
				for($i = 0;$i<$message_len;$i++){
					$class = $sql->table($table)->where(array("cla_id"=>$d["message"][$i]["cla_id"]))->field(array("cla_name"))->find();
					if($class){
						$d["message"][$i]["cla_name"] = $class["cla_name"];
					} else{
						$d["message"][$i]["cla_name"] = null;
					}
				}
				unset($message_len,$table);
			}
			$data = $this->message(1,"获取通知信息成功!",$d);
			$this->ajaxReturn($data);
			unset($data);
		}
	}

	/*
	获取未读信息  
	ajax 本地
	 */
	public function notMessage(){
		if(IS_AJAX && IS_LOCAL){
			session_start();
			$stu_id = $_SESSION["id"];
			session_write_close();
			$sql = D();
			$d["message"] = $sql->table(array("message"=>""))->where(array("stu_id"=>$stu_id,"m_status"=>0))->field(array("m_id","m_status","m_content","m_date","cla_id"))->order("m_date desc")->select();
			if($d["message"]){
				$message_len = count($d["message"]);
				$table = array("class"=>"");
				for($i = 0;$i<$message_len;$i++){
					$class = $sql->table($table)->where(array("cla_id"=>$d["message"][$i]["cla_id"]))->field(array("cla_name"))->find();
					if($class){
						$d["message"][$i]["cla_name"] = $class["cla_name"];
					} else{
						$d["message"][$i]["cla_name"] = null;
					}
				}
				unset($message_len,$table);
			}
			$data = $this->message(1,"获取通知信息成功!",$d);
			$this->ajaxReturn($data);
			unset($data,$d);
		}
	}

	/*
	读取某个信息
	ajax  本地  m_id
	 */
	public function read(){
		if(IS_AJAX && IS_LOCAL && isset($_POST["m_id"])){
			session_start();
			$stu_id = $_SESSION["id"];
			session_write_close();
			$sql = D();
			$table = array("message"=>"");
			$message = $sql->table($table)->where(array("stu_id"=>$stu_id,"m_id"=>$_POST["m_id"]))->field(array("m_id","m_status","m_content","m_date","cla_id"))->order("m_date desc")->find();
			if($message){
				$sql->table($table)->where(array("stu_id"=>$stu_id,"m_id"=>$_POST["m_id"]))->field(array("m_status"))->setInc(1);
				$class = $sql->table(array("class"=>""))->where(array("cla_id"=>$message["cla_id"]))->field(array("cla_name"))->find();
				if($class){
					$message["cla_name"] = $class["cla_name"];
				} else{
					$message["cla_name"] = null;
				}
				$data = $this->message(1,"读取通知信息成功!",$message);
				unset($class);
			} else{
				$data = $this->message(0,"非法操作!","");
			}
			$this->ajaxReturn($data);
			unset($stu_id,$sql,$table,$message,$data);
		}
	}

	/*
	删除所有已经读了的信息  
	ajax  本地
	 */
	public function delete(){
		if(IS_AJAX && IS_LOCAL){
			session_start();
			$stu_id = $_SESSION["id"];
			session_write_close();
			if(D()->table(array("message"=>""))->where(array("stu_id"=>$stu_id,"m_status"=>1))->delete()){
				$data = $this->message(1,"删除通知成功!","");
			} else{
				$data = $this->message(0,"删除通知失败!","");
			}
			$this->ajaxReturn($data);
			unset($data,$stu_id);
		}
	}

	/*
	将所有未读变成已读
	ajax  本地
	 */
	public function change(){
		if(IS_AJAX && IS_LOCAL){
			session_start();
			$stu_id = $_SESSION["id"];
			session_write_close();
			$sql = D();
			if($sql->table(array("message"=>""))->where(array("stu_id"=>$stu_id,"m_status"=>"0"))->field(array("m_status"))->setInc(1)){
				$data = $this->message(1,"通知标志为已读成功!","");
			} else{
				$data = $this->message(0,"通知标志为已读失败!","");
			}
			$this->ajaxReturn($data);
			unset($data,$stu_id);
		}
	}

    //析构函数不用写任何内容也是可以的
	public function __destruct(){}
}